﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using TiTi.tea.Api.Models;
using TiTi.tea.BLL;
using TiTi.tea.Model;

namespace TiTi.tea.Api.Controllers
{
    [RoutePrefix("api/Template")]
    public class TemplateController : ApiController
    {
        private TemplateBLL bll = new TemplateBLL();


        [Route("Query")]
        [HttpGet]

        public Result Query(string tplName = "", int PageSize = 10, int PageIndex = 1)
        {
            int total = 0;
            var data = bll.Query(tplName, out total, PageSize, PageIndex);
            return Result.Success(new { data, total });
        }
        [Route("Get")]
        [HttpGet]
        public Result Get(int Id)
        {
            return Result.Success(bll.GetOne(Id));
        }

        /// <summary>
        /// 获取全部模板
        /// </summary>
        /// <param name="tplName">模板名称</param>
        /// <returns></returns>
        [Route("GetAll")]
        [HttpGet]
        public Result GetAll(string tplName = "")
        {
            return Result.Success(bll.GetAll(tplName));
        }
        [Route("Add")]
        [HttpPost]
        public Result Add(Template entity)
        {
            entity.IsDelete = 0;
            if (bll.Add(entity))
            {

                return Result.Success();
            }
            else
            {
                return Result.Error("添加失败");
            }
        }

        [Route("Update")]
        [HttpPost]
        public Result Update(Template entity)
        {
            if (entity.Id == 0)
            {
                return Result.Error("Id不能为空");
            }
            else
            {
                Template template = bll.GetOne(entity.Id);

                template.TplName = entity.TplName;

                template.IsDelete = entity.IsDelete;

                bool num = bll.Update(template);
                if (num)
                {
                    return Result.Success();
                }
                else
                {
                    return Result.Error("修改失败，仔细请查看字符是否出错");
                }
            }
        }

        [Route("Delete")]
        [HttpPost]
        public Result Delete(int Id)
        {
            Template template = bll.GetOne(Id);
            template.IsDelete = 1;
            bool num = bll.Update(template);
            if (num)
            {
                return Result.Success();
            }
            else
            {
                return Result.Error("删除失败");
            }
        }


       
    }
}